/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.ModelDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class ModelFacade extends AbstractFacade<Model> implements ModelFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ModelFacade() {
        super(Model.class);
    }
    
    @Override
    public Integer createModel (String name, String code) {
        Model model = new Model();
        model.setName(name);
        model.setCode(code);
        create(model);
        return model.getId();
    }
    
    @Override
    public void editModel (Integer id, String name, String code) {
        Model model = find(id);
        model.setName(name);
        model.setCode(code);
        edit(model);
    }
    
    @Override
    public void removeModel (Integer id){
        Model model = find(id);
        remove(model);
    }
    
    @Override
    public ModelDetails getModels(Integer id){
        return convert(find(id));
    }
    
    @Override
    public Collection<ModelDetails> getAllModelDetails(){
        Collection<ModelDetails> models = new ArrayList<ModelDetails>();
        for(Model model:findAll()){
            models.add(convert(model));
        }
        return models;
    }
    
    @Override
    public Model createModel(ModelDetails md, Article a){
        Model model = new Model();
        model.setName(md.getName());
        model.setCode(md.getCode());
        model.setArticleid(a);
        create(model);
        return model;
    }
    
    @Override
    public ModelDetails convert (Model model){
        if(model == null)
            return null;
        return new ModelDetails(model.getId(), model.getName(), model.getCode());
    }
    
    @Override
    public Collection<ModelDetails> convert (Collection<Model> models){
        Collection<ModelDetails> details = new ArrayList<ModelDetails>();
        for(Model model : models){
            details.add(convert(model));
        }
        return details;
    }
    
    
    
}
